;
(function() {
  new Vue({
    el: '#report_bi',
    data: {
      currNumber: ['10'],
      page: 1,
      pageTcount: [{name: '10', code: '10'}, {name: '15', code: '15'}, {name: '20', code: '20'}, {name: '30', code: '30'}],
			projectId: '',
			buildingArr: [],
			buildingId: [],
			floorArr: [],
			floorId: [],
			unitArr: [],
			unitId: [],
			companyArr: [],
			company: '',
			companyType: '#',
			gddstReportObj: {},
			biObj: {},
			indexArr: [],
			power: JSON.parse(localStorage.getItem('permissions')),
			isMatch: 'N'
    },
    filters: {
      toZero: function (n) {
        return n < 10 ? '0' + n : '' + n
      },
      date: function (time) {
				if (!time) return '--';
        return hlDateUtil.formatUTCDate(time, 'yyyy-MM-dd')
      },
      formatNum:function (num) {
        return hlNumberUtil.formatNum(num)
      },
    },
    methods: {
			//获取使用单位列表
			leaseHolderTypeDt () {
				this.isMatch = 'N'
				hlhttp.get({
					url: 'project/lease_holder_types',
					data: {
						keyword: this.company || '#'
					},
					success: (data) => {
						data.map(item => {
							item.title = item.leaseHolderType.length > 6 ? item.leaseHolderType : null;
						})
						this.companyArr = data
					}
				})
			},
			//统计报表数据
			gddstReportDt () {
				hlhttp.get({
					url: 'project/gddst_report/_statistics',
					data: {
						building_id: this.buildingId.length ? this.buildingId[0] : '#',
						floor_id: this.floorId.length ? this.floorId[0] : '#',
						unit_id: this.unitId.length ? this.unitId[0] : '#',
						leaseholder_type: this.isMatch == 'N' ? this.company ? this.company : '#' : this.companyType,
						is_accurate_matching: this.isMatch
					},
					success: (data => {
						this.gddstReportObj = data;
					})
				})
			},
      //获取报表
			biGDRZ () {
				this.gddstReportDt()
				hlhttp.get({
					url: 'project/gddst_report/_search',
					data: {
						building_id: this.buildingId.length ? this.buildingId[0] : '#',
						floor_id: this.floorId.length ? this.floorId[0] : '#',
						unit_id: this.unitId.length ? this.unitId[0] : '#',
						leaseholder_type: this.isMatch == 'N' ? this.company ? this.company : '#' : this.companyType,
						page: this.page,
						page_count: this.currNumber[0],
						is_accurate_matching: this.isMatch
					},
					success: (data) => {
						let index = 0;
						data.buildings.map(item => {
							item.units.map(item0 => {
								index++;
								item0.index = index
							})
						})
						this.biObj = data;
						$("#page").initPage(data.totalPage, this.page, (n) => {
						  if (this.page == n) {
						    return
						  }
							this.page = n;
						  this.biGDRZ();
						});
					}
				})
			},
			//失去焦点
			blur () {
				setTimeout(() => {
					this.companyArr = []
				}, 300)
			},
			//使用单位
			changeStr () {
				setTimeout(() => {
					this.leaseHolderTypeDt()
				}, 300)
			},
			//选择单位
			selectedDw (item) {
				this.companyType = item.leaseHolderType;
				this.company = item.leaseHolderType;
				this.isMatch = 'Y'
			},
			//重置
			reset () {
				this.buildingId = [];
				this.floorId = [];
				this.unitId = [];
				this.companyType = '#';
				this.company = '';
				this.projectId = '';
				this.page = 1;
				this.currNumber = ['10'];
				this.floorArr = [];
				this.unitArr = [];
				this.biGDRZ();
			},
			//数量控制
			pageNumber (arr) {
				this.currNumber = [arr[0].code];
				this.biGDRZ();
			},
			//获取楼栋
			buildingDt () {
				hlhttp.get({
					url: 'tenant/project/buildings',
					success: (data) => {
						this.buildingArr = hlArrayUtil.replaceData(data, [{key: 'buildingId', rKey: 'code'}]);
					}
				})
			},
			//获取楼层
			floorDt () {
				hlhttp.get({
					url: 'building/floors',
					data: {project_id: this.projectId[0], building_id: this.buildingId[0]},
					success: (data) => {
						this.floorArr = hlArrayUtil.replaceData(data, [{key: 'floorName', rKey: 'name'}, {key: 'floorId', rKey: 'code'}]);
					}
				})
			},
			//获取单元
			unitDt () {
				hlhttp.get({
					url: 'floor/units',
					data: {project_id: this.projectId[0], building_id: this.buildingId[0], floor_id: this.floorId[0]},
					success: (data) => {
						this.unitArr = hlArrayUtil.replaceData(data, [{key: 'unitName', rKey: 'name'}, {key: 'unitId', rKey: 'code'}]);
					}
				})
			},
			//选择楼栋
			selectBuilding (arr) {
				this.buildingId = [arr[0].buildingId];
				this.projectId = [arr[0].projectId];
				this.floorDt()
			},
			//选择楼层
			selectFloor (arr) {
				this.floorId = [arr[0].floorId];
				this.unitDt()
			},
			//选择单元
			seletUnit (arr) {
				this.unitId = [arr[0].unitId];
			},
			//导出文件
			incomeExport() {
			  hlhttp.get({
			    baseUrl: exclUrl,
			    url: 'resource/gddst_report_export',
			    data: {
			      building_id: this.buildingId.length ? this.buildingId[0] : '#',
			      floor_id: this.floorId.length ? this.floorId[0] : '#',
			      unit_id: this.unitId.length ? this.unitId[0] : '#',
			      leaseholder_type: this.isMatch == 'N' ? this.company ? this.company : '#' : this.companyType,
			      is_accurate_matching: this.isMatch
			    },
			    success: (data) => {
			      var a = document.createElement('a');
			      a.href = data.excel_url;
			      $('body').append($(a))
			      a.click();
			      setTimeout(function() {
			        $(a).remove()
			      }, 100);

			    }
			  })
			},
    },
    mounted () {
			this.biGDRZ()
			this.buildingDt()
			hlSelectUi()
		}
  })
}())
